package com.sanery.util;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadHelper {

	public static Map<String, Object> upload(HttpServletRequest request, long max, String allowtype) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		request.setCharacterEncoding("utf-8");
		if (ServletFileUpload.isMultipartContent(request)) {
			DiskFileItemFactory fty = new DiskFileItemFactory();
			// fty.setRepository(new File("D:\\uploadtmp"));
			// fty.setSizeThreshold(4*1024);
			ServletFileUpload upload = new ServletFileUpload(fty);// 上传核心组件
			if (max > 0)
				upload.setSizeMax(max);
			List<FileItem> items = upload.parseRequest(request);// 分解上传上来的东西
			for (FileItem item : items) {
				if (item.isFormField())
					map.put(item.getFieldName(), item.getString("utf-8"));
				else {
					String fname = item.getName();
					if (fname == null || fname.isEmpty())
						continue;
					String ext = fname.substring(fname.lastIndexOf('.') + 1);// 那他的后缀名
					if (allowtype != null && allowtype.indexOf(ext) == -1)
						throw new RuntimeException("不符合上传格式");

					byte[] bytes = item.get();
					item.delete();
					map.put(item.getFieldName(), bytes);// 返回二进制数组
					map.put("filename", fname);
				}
			}
		}
		return map;
	}

	public static Map<String, String> upload(HttpServletRequest request, String dirpath// 文件保存到哪里去
			, int max// 不能超过多大
			, String allowtype// 允许什么类型
	) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		String dir = request.getSession().getServletContext().getRealPath(dirpath); // /imgs-->物理路径
		request.setCharacterEncoding("utf-8");
		if (ServletFileUpload.isMultipartContent(request)) {
			DiskFileItemFactory fty = new DiskFileItemFactory();
			// fty.setRepository(new File("D:\\uploadtmp"));
			// fty.setSizeThreshold(4*1024);
			ServletFileUpload upload = new ServletFileUpload(fty);// 上传核心组件
			upload.setSizeMax(max);// 设置最大大小
			List<FileItem> items = upload.parseRequest(request);// 分解上传上来的东西
			for (FileItem item : items) {
				if (item.isFormField())
					map.put(item.getFieldName(), item.getString("utf-8"));
				else {
					String fname = item.getName();
					if (fname == null || fname.isEmpty())
						continue;
					String ext = fname.substring(fname.lastIndexOf('.') + 1);// 那他的后缀名
					if (allowtype.indexOf(ext) == -1)
						throw new RuntimeException("不符合图片格式");
					String filename = new Date().getTime() + "." + ext;
					item.write(new File(dir + "/" + filename));
					byte[] imgByte = item.get();
					item.delete();// 清空临时文件夹(过渡用的)中的数据
					map.put(item.getFieldName(), filename);// 把处理好的文件名放字典，后面要用到
				}
			}
		}
		return map;
	}

	public static Map<String, String> upload2(HttpServletRequest request, String dir// 文件保存的绝对路径
			, int max// 不能超过多大
			, String allowtype// 允许什么类型
	) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		request.setCharacterEncoding("utf-8");
		if (ServletFileUpload.isMultipartContent(request))// 校验一下是不是对应的格式
		{
			DiskFileItemFactory fty = new DiskFileItemFactory();
			// fty.setRepository(new File("D:\\uploadtmp"));
			// fty.setSizeThreshold(4*1024);
			ServletFileUpload upload = new ServletFileUpload(fty);// 上传核心组件
			upload.setSizeMax(max);// 设置最大大小
			List<FileItem> items = upload.parseRequest(request);// 分解上传上来的东西
			for (FileItem item : items) {
				if (item.isFormField())// 是不是普通的文本，等效于request.getParameter("")
					map.put(item.getFieldName(), item.getString("utf-8"));
				else {
					String fname = item.getName();
					if (fname == null || fname.isEmpty())
						continue;
					String ext = fname.substring(fname.lastIndexOf('.') + 1);// 拿他的后缀名.jpg,.png
					if (allowtype != null && allowtype.indexOf(ext) == -1)
						throw new RuntimeException("不符合图片格式");
					String filename = new Date().getTime() + "." + ext;
					item.write(new File(dir + "/" + filename));
					// byte[] imgByte=item.get();
					item.delete();// 清空临时文件夹(过渡用的)中的数据
					map.put(item.getFieldName(), filename);// 把处理好的文件名放字典，后面要用到
				}
			}
		}
		return map;
	}
}
